package com.sdp.core.bizc.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

@Component
public class TokenUtils {
	
    
    
	private static long expireDate;
    
    
    private static String tokenSecret;
    
    public static String token (String accountNo,String accountPwd,String empNo,String empName){
    	String token = "";
    	try {
    		Date date = new Date(System.currentTimeMillis()+expireDate);
    		//秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(tokenSecret);
            
           //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");
            token = JWT.create()
                    .withHeader(header)
                    .withClaim(CoreConstantsEnum.token中账号的属性名.getCode(),accountNo)
                    .withClaim(CoreConstantsEnum.token中工号的属性名.getCode(),empNo)
                    .withClaim(CoreConstantsEnum.token中密码的属性名.getCode(),accountPwd)
                    .withClaim(CoreConstantsEnum.token中姓名的属性名.getCode(),empName)
                    .withExpiresAt(date)
                    .sign(algorithm);
		} catch (Exception e) {
			//e.printStackTrace();
            return null;
		}
    	return token;
    }
    
    public static JSONObject verify(String token){
        JSONObject obj = new JSONObject();
        try {
            Algorithm algorithm = Algorithm.HMAC256(tokenSecret);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            Map<String, Claim> claims = jwt.getClaims();
            
            if (claims.containsKey(CoreConstantsEnum.token中账号的属性名.getCode()) 
            		&& claims.containsKey(CoreConstantsEnum.token中工号的属性名.getCode())
            		&& claims.containsKey(CoreConstantsEnum.token中密码的属性名.getCode())
            		&& claims.containsKey(CoreConstantsEnum.token中姓名的属性名.getCode())) {
            	
            	String accountNo = claims.get(CoreConstantsEnum.token中账号的属性名.getCode()).asString();
            	String empNo = claims.get(CoreConstantsEnum.token中工号的属性名.getCode()).asString();
            	String empPwd = claims.get(CoreConstantsEnum.token中密码的属性名.getCode()).asString();
            	String empName = claims.get(CoreConstantsEnum.token中姓名的属性名.getCode()).asString();
            	
            	obj.put(CoreConstantsEnum.token中账号的属性名.getCode(), accountNo);
            	obj.put(CoreConstantsEnum.token中工号的属性名.getCode(), empNo);
            	obj.put(CoreConstantsEnum.token中密码的属性名.getCode(), empPwd);
            	obj.put(CoreConstantsEnum.token中姓名的属性名.getCode(), empName);
            	obj.put(CoreConstantsEnum.token中的时间属性名.getCode(), jwt.getExpiresAt());
            }
        }catch (Exception e){
            e.printStackTrace();
            obj=null;
        }
        return obj;
    }
    
    
    
    @Value("${sdp.expireDate}")
	public void setExpireDate(long expireDate) {
		TokenUtils.expireDate = expireDate;
	}


	@Value("${sdp.tokenSecret}")
	public void setTokenSecret(String tokenSecret) {
		TokenUtils.tokenSecret = tokenSecret;
	}

}
